package com.ylb.rent.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ylb.rent.config.PageConfig;
import com.ylb.rent.util.EsUtil;
import com.ylb.rent.util.R;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;

import com.ruoyi.common.enums.BusinessType;
import  com.ylb.rent.entity.School;
import com.ylb.rent.service.ISchoolService;



/**
 * 学校Controller
 * 
 * @author ylb
 * @date 2023-06-29
 */
@RestController
@RequestMapping("/school/school")
public class SchoolController
{
    @Autowired
    private ISchoolService schoolService;

    @Autowired
    private EsUtil esUtil;
    /**
     *  模糊搜索
     *
     * */

    @GetMapping("/search/keyword")
    public  String searchByKeyword(String keyword){
        List<School> schoolName = new ArrayList<>();
        schoolName = esUtil.keywordSearch("schoolName", keyword, School.class);
        return R.successJSONData(schoolName);
    }
    /**
     * 查询学校列表
     */
    @PreAuthorize("@ss.hasPermi('school:school:list')")
    @GetMapping("/list")
    public String list(School school, PageConfig pageConfig)
    {
        PageHelper.startPage(pageConfig);
        List<School> list = schoolService.selectSchoolList(school);
        return R.successJSONData(new PageInfo<>(list));

    }

    /**
     * 获取学校详细信息
     */
    @PreAuthorize("@ss.hasPermi('school:school:query')")
    @GetMapping(value = "/{id}")
    public String getInfo(@PathVariable("id") Long id)
    {
        return R.successJSONData(schoolService.selectSchoolById(id));
    }

    /**
     * 新增学校
     */
    @PreAuthorize("@ss.hasPermi('school:school:add')")
    @Log(title = "学校", businessType = BusinessType.INSERT)
    @PostMapping
    public String add(@RequestBody School school)
    {
        return R.successJSONData(schoolService.insertSchool(school));
    }

    /**
     * 修改学校
     */
    @PreAuthorize("@ss.hasPermi('school:school:edit')")
    @Log(title = "学校", businessType = BusinessType.UPDATE)
    @PutMapping
    public String edit(@RequestBody School school)
    {
        return R.successJSONData(schoolService.updateSchool(school));
    }

    /**
     * 删除学校
     */
    @PreAuthorize("@ss.hasPermi('school:school:remove')")
    @Log(title = "学校", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public String remove(@PathVariable Long[] ids)
    {
        return R.successJSONData(schoolService.deleteSchoolByIds(ids));
    }
}
